<?php

namespace app\common\model;

class ShopExtendModel extends BaseModel
{
    protected $name = 'shop_extend';
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改
    protected $type = ['extend_value' => 'json'];

    /**
     * 获取搜索属性html
     * @param $cid
     * @param int $goodsId
     * @return string
     */
    public function getExtendHtml($cid, $goodsId = 0)
    {
        $extendInfo = $this->where('cid', $cid)->where('extend_status', 1)->find();
        if (empty($extendInfo)) {
            return '';
        }
        $html = "<div class=\"form-group\">" . PHP_EOL;
        if ($goodsId > 0) {
            $shopGoodsExtendModel = new ShopGoodsExtendModel();
            $goodsExtend          = $shopGoodsExtendModel->where('goods_id', $goodsId)->column('id,item_select', 'item_name');
        }
        $i    = 0;
        $html .= "<tr>" . PHP_EOL;
        foreach ($extendInfo['extend_value'] as $v) {
            $i++;
            if (!empty($goodsExtend)) {
                $defaultValue = $goodsId > 0 ? $goodsExtend[$v['item_name']]['item_select'] : $v['item_default'];
            } else {
                $defaultValue = $v['item_default'];
            }
            $formControl = $this->format($v, $defaultValue); //当前表单
            if ($goodsId > 0) {
                $html .= "<input type=\"hidden\" name=\"extend[{$v['item_name']}][id]\" value=\"{$goodsExtend[$v['item_name']]['id']}\" />" . PHP_EOL;
            }
            $html .= "<input type=\"hidden\" name=\"extend[{$v['item_name']}][extend_id]\" value=\"{$extendInfo['extend_id']}\" />" . PHP_EOL;
            $html .= "<input type=\"hidden\" name=\"extend[{$v['item_name']}][cid]\" value=\"{$extendInfo['cid']}\" />" . PHP_EOL;
            $html .= "<input type=\"hidden\" name=\"extend[{$v['item_name']}][cid_path]\" value=\"{$extendInfo['cid_path']}\" />" . PHP_EOL;
            $html .= "<input type=\"hidden\" name=\"extend[{$v['item_name']}][item_name]\" value=\"{$v['item_name']}\" />" . PHP_EOL;
            $html .= "<input type=\"hidden\" name=\"extend[{$v['item_name']}][item_value]\" value=\"{$v['item_value']}\" />" . PHP_EOL;
            $html .= "<label class=\"col-sm-2 control-label Validform_label\">{$v['item_name']}</label>" . PHP_EOL;
            $html .= " <div class=\"col-sm-4\">{$formControl}</div>" . PHP_EOL;
            if ($i % 2 == 0) {
                $html .= "</div><div class=\"form-group\">" . PHP_EOL;
            }
        }
        if ($i % 2 == 1) {
            $html .= "</div>" . PHP_EOL;
        }
        return $html;
    }

    /**
     * 格式化下拉框
     */
    private function format($itemInfo, $defaultValue)
    {
        $options = "<option value=\"\">请选择</option>" . PHP_EOL;
        if (!empty($itemInfo['item_value'])) {
            $values = explode(',', $itemInfo['item_value']);
            foreach ($values as $value) {
                $selected = $value == $defaultValue ? 'selected' : '';
                $options  .= "<option value=\"{$value}\" $selected>{$value}</option>" . PHP_EOL;
            }
        }
        $html = "<select class=\"form-control\" name=\"extend[{$itemInfo['item_name']}][item_select]\">" . PHP_EOL;
        $html .= $options . PHP_EOL;
        $html .= "</select>" . PHP_EOL;
        return $html;
    }

}
